const app = getApp()
let host = app.globalData.host;
Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  //表单提交
  formSubmit(e){

    let {nickname,phone} = e.detail.value
    wx.request({
      url: host+'/updateAdmin',
      data: {
        nickname: nickname,
        phone: phone,
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },success (res) {
      console.log(res.data)
      },
      fail (error){
        console.log("失败")
      }
    })
    wx.redirectTo({
      url: '../admins/admins',
    })

  },
  formSubmitNew(e){
    let {openid,nickname,phone,code} = e.detail.value
    wx.request({
      url: host+'/addAdmin',
      data: {
        openid: openid,
        nickname: nickname,
        phone: phone,
        code: code
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },success (res) {
      console.log(res.data)
      },
      fail (error){
        console.log("失败")
      }
    })
    wx.redirectTo({
      url: '../admins/admins',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this

    if (app.globalData.employId && app.globalData.employId != '') {
      let employId = app.globalData.employId
        wx.request({
            url: 'http://localhost:8080'+'/showAdmin?openid='+employId,
            method: "GET",
            header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
             },success (res) {
              if (res.data.openid != null) {
                that.setData({
                  isAdmin: true,
                  openid: res.data.openid,
                  nickname: res.data.nickname,
                  phone: res.data.phone
                })
              } else {
                that.setData({
                  openid: employId,
                })
              }
             },
          fail (error){
             console.log("失败")
          }
      })
    } else {
      app.employIdCallback = employId => {
        if (employId != '') {
          wx.request({
            url: 'http://localhost:8080'+'/showAdmin?openid='+employId,
            method: "GET",
            header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
             },success (res) {
              if (res.data.openid != null) {
                that.setData({
                  isAdmin: true,
                  openid: res.data.openid,
                  nickname: res.data.nickname,
                  phone: res.data.phone
                })
              } else {
                that.setData({
                  openid: employId,
                })
              }
          },
          fail (error){
             console.log("失败")
          }
      })
        }
      }
    }
  },
})